package com.jwgf.smarowindingmachine_unity.ui;

import com.jwgf.smarowindingmachine_unity.R;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.fragment.app.DialogFragment;

public class ListDialogFragment extends DialogFragment {
	public static final String ARRAY = "array";

	private AlertDialog dlg;
	private ListView listView;
	private OnItemClickListener itemClickListener;

	public void setOnItemClickListener(OnItemClickListener itemClickListener) {
		this.itemClickListener = itemClickListener;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View v = inflater.inflate(R.layout.list_view, null);
		Bundle bd = getArguments();
		String[] items = bd.getStringArray(ARRAY);
		listView = (ListView) v.findViewById(R.id.list);
		listView.setAdapter(new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, items));
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				itemClickListener.onItemClick(parent, view, position, id);
				dismiss();
			}
		});
		builder.setTitle(R.string.select);
		builder.setView(v);
		dlg = builder.create();
		return dlg;
	}

}
